﻿/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#pragma once

#include <QWidget>
#include <QSpinBox>
#include <QDockWidget>
#include "JistCommonModel.h"

#include "JistCommonDelegate.h"
//#include "JistTableView.h"
#include "../libtiled/map.h"
#include "../../libtiled/object.h"
//class Map;

class JistTableView;
class FigureEditor;
QT_FORWARD_DECLARE_CLASS(QUndoStack)
QT_FORWARD_DECLARE_CLASS(QTextStream)

extern     QImage g_EmptyImage;

class JistDocument : public QWidget
{
public:
    enum SubTableType
    {
        IMAGE,
        SPRITE,
        ANIMATION,
        SCENE,
        CELL
    };

    Q_OBJECT

public:
    JistDocument(QWidget *parent = 0);
    ~JistDocument();
    QUndoStack *undoStack() const;
    JistCommonModel* getSpriteListModel();
    QString m_strDocName;

    void setSpriteFrame(QFrame *widget);
    void setSpriteDelegate(JistCommonDelegate *delegate);
    void setSpriteView(QModelIndex midx);
    bool addSpriteList(QString path = QString());
    void setSubTableView(JistTableView *tableWidget, SubTableType type);
    void setSrcFigure(FigureEditor *editor);
    void setAniFigure(FigureEditor *editor);

    void changeSpriteList(int index);
    void changeSceneList(int index);
    void changeCellList(int index);
    void changeCellData( const QItemSelection & selected, const QItemSelection & deselected );
    void addAnimation(QString name =  QString(), bool isLoading = false);
    void addScene(bool isLoad = true);

    // 언두리두관련
    void setDocName(QString fileName);
    QString getDocName();
    bool isModified() const;

    // 저장
    void saveFile(QString path =  QString());
    void saveTempFile(QString path);
	void saveAniUnion(QIODevice *device, QString &path);
    void saveAniUnion(QXmlStreamWriter &w, const QString &path);
    void saveAniUnionSeveral(QXmlStreamWriter &w, const QString &path);
    void saveImageData(QXmlStreamWriter &w, const JistCommonData &data, int index);
    void saveSpriteData(QXmlStreamWriter &w, const JistCommonData &data);

    void saveAnimationData(QXmlStreamWriter &w, const JistCommonData &data, int index);
    void saveSceneData(QXmlStreamWriter &w, const JistCommonData &data, int index,  int sceneIdx);
    void saveCellData(QXmlStreamWriter &w, QList< JistCommonData > &listData, int index);
    void saveCellDataToBinary(QXmlStreamWriter &w, QList< JistCommonData > &listData, int index);

    // 로드
    bool loadFile(QString path =  QString());
    void loadImage(QIODevice *device);
    void loadImageOnTiled(QIODevice *device, Tiled::Map *map);
    void loadImage(QString imgSetPath =  QString());
    JistCommonData* loadSprite(QImage *img);

    void loadAnimation();
    void loadScene();
    void loadCell(int layNo);

    void newDocument();

    void clearAniList();

    FigureEditor* getSpriteFigure();
    void loadImageSet(QIODevice *device, QString imgSetPath =  QString());

    void setScaleControl(QSpinBox * spX, QSpinBox * spY, QSlider *slX, QSlider *slY);

    JistCommonData* getCurrentSpriteData();
    void ClearList();
//    void SetMapDocument(Tiled::Internal::DocumentManager *mapDocMan) { m_pMapDocMan = mapDocMan; }
//    Tiled::Internal::DocumentManager * GetMapDocumet() { return m_pMapDocMan; }

protected:
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

signals:
    /**
     * Emitted when the current displayed map document changed.
     */
    void currentDocumentChanged(/*MapDocument *mapDocument*/);
    void changeSelectCell(JistCommonData *data);
    void changeSelectCell(QModelIndexList list);
    void changeSelectSprite(QModelIndexList list);

public slots:
    void addCell(bool isEmpty = false);
    void addShadowCell(int iX, int iY);
    void OpenImage();
    ImageItem* getCurrentCell();
    void setCellAlpha(int val);
    void setCellScale(int val);
    void setCellScaleX(int val);
    void setCellScaleY(int val);
    void setCellRotate(int val);
    void setCellMirror(bool horizontally, bool vertically);

    void setClearMode() {setCellEffect(QPainter::CompositionMode_Clear); }
    void setSourceMode() {setCellEffect(QPainter::CompositionMode_Source); }
    void setDestMode() {setCellEffect(QPainter::CompositionMode_Destination); }
    void setSourceOverMode() {setCellEffect(QPainter::CompositionMode_SourceOver); }
    void setDestOverMode() {setCellEffect(QPainter::CompositionMode_DestinationOver); }
    void setSourceInMode() {setCellEffect(QPainter::CompositionMode_SourceIn); }
    void setDestInMode() {setCellEffect(QPainter::CompositionMode_DestinationIn); }
    void setSourceOutMode() {setCellEffect(QPainter::CompositionMode_SourceOut); }
    void setDestOutMode() {setCellEffect(QPainter::CompositionMode_DestinationOut); }
    void setSourceAtopMode() {setCellEffect(QPainter::CompositionMode_SourceAtop); }
    void setDestAtopMode() {setCellEffect(QPainter::CompositionMode_DestinationAtop); }
    void setXorMode() {setCellEffect(QPainter::CompositionMode_Xor); }

    void setPlusMode() {setCellEffect(QPainter::CompositionMode_Plus);}
    void setMultiplyMode() {setCellEffect(QPainter::CompositionMode_Multiply); }
    void setScreenMode() {setCellEffect(QPainter::CompositionMode_Screen); }
    void setOverlayMode() {setCellEffect(QPainter::CompositionMode_Overlay); }
    void setDarkenMode() {setCellEffect(QPainter::CompositionMode_Darken); }
    void setLightenMode() {setCellEffect(QPainter::CompositionMode_Lighten); }
    void setColorDodgeMode() {setCellEffect(QPainter::CompositionMode_ColorDodge); }
    void setColorBurnMode() {setCellEffect(QPainter::CompositionMode_ColorBurn); }
    void setHardLightMode() {setCellEffect(QPainter::CompositionMode_HardLight); }
    void setSoftLightMode() {setCellEffect(QPainter::CompositionMode_SoftLight); }
    void setDifferenceMode() {setCellEffect(QPainter::CompositionMode_Difference); }
    void setExclusionMode() {setCellEffect(QPainter::CompositionMode_Exclusion); }
    void setColor0Mode() {setCellEffect(100); }
    void setColor1Mode() {setCellEffect(101); }
    void setColor2Mode() {setCellEffect(102); }
    void setColor3Mode() {setCellEffect(103); }
//    void setOriginPosX1() { if(!getCurrentCell()) return; getCurrentCell()->setOriginPosInit(0,getCurrentCell()->m_oIndex_Y);}
//    void setOriginPosX2() { if(!getCurrentCell()) return; getCurrentCell()->setOriginPosInit(1,getCurrentCell()->m_oIndex_Y);}
//    void setOriginPosX3() { if(!getCurrentCell()) return; getCurrentCell()->setOriginPosInit(2,getCurrentCell()->m_oIndex_Y);}
//    void setOriginPosY1() { if(!getCurrentCell()) return; getCurrentCell()->setOriginPosInit(getCurrentCell()->m_oIndex_X, 0);}
//    void setOriginPosY2() { if(!getCurrentCell()) return; getCurrentCell()->setOriginPosInit(getCurrentCell()->m_oIndex_X, 1);}
//    void setOriginPosY3() { if(!getCurrentCell()) return; getCurrentCell()->setOriginPosInit(getCurrentCell()->m_oIndex_X, 2);}

    void setOriginPosX1();
    void setOriginPosX2();
    void setOriginPosX3();
    void setOriginPosY1();
    void setOriginPosY2();
    void setOriginPosY3();

    void setAnchorTextItem(int index);
    QModelIndexList getSelectCellList();
    QList<QGraphicsItem*> getSelectItemList();
    void setCellEffect(int eff);

public slots:
    void currentIndexChanged();

private:
    int m_currentIndex;
    int m_mousePressIndex;
    QPoint m_mousePressOffset;
    bool m_resizeHandlePressed;
    QString m_fileName;

    QUndoStack *m_undoStack;

    JistCommonDelegate *m_pSpriteDelegate;
    JistCommonDelegate *m_pCellDelegate;
//    Tiled::Map *m_Currentmap;
//  Tiled::Internal::MapDocument *m_MapDocument;
//  Tiled::Internal::DocumentManager *m_pMapDocMan;

public:

    JistCommonModel m_ImageListModel;
    JistCommonModel m_SpriteListModel;

    JistCommonModel m_AniListModel;
    JistCommonModel m_SceneListModel;
    JistCommonModel m_CellListModel;

    QList< JistCommonData > m_ImageData;
    QVector< QList < JistCommonData> > m_SpriteData;

    QList< JistCommonData > m_AnimationData;
    QVector< QList < JistCommonData > > m_SceneData;
    QVector< QVector< QList< JistCommonData > > > m_CellData;
    QVector< JistCellData> m_Binary;

    int m_iCurrentSpriteDataIndex;
    int m_iCurrentSceneDataIndex;
    int m_iPreSceneDataIndex;
    int m_iCurrentCellDataIndex;

    FigureEditor *m_SpriteUnitFigure;
    FigureEditor *m_pSrcFigure;
    FigureEditor *m_pSceneFrameFigure;
    FigureEditor *m_pAnimationFigure;

    JistTableView *m_ImageTableWidget;
    JistTableView *m_SpriteTableWidget;

    JistTableView *m_AnimaionTableWidget;
    JistTableView *m_SceneTableWidget;
    JistTableView *m_CellTableWidget;

    QFrame *m_pFrameSprite;

    QXmlStreamReader xml;
    float m_fCurrentVersion;
    float m_fLoadingVersion;
    QString m_FinalWorkPath;
    int m_iEnermyMonType;
    QModelIndexList m_selectCellList;
    bool m_isBG_EditMode;
    bool m_isLoading;
    bool m_isAddedResource;
    int m_iSavigAniIdnex;

    QSpinBox *m_ScaleXSpinBox;
    QSpinBox *m_ScaleYSpinBox;
    QSlider *m_ScaleXHorizontalSlider;
    QSlider *m_ScaleYHorizontalSlider;

    QList<QGraphicsItem*> m_BeVisibleItemList;

    QDockWidget * m_pSceneViewDockWidget;

    bool m_isAddingBinData;
    Tiled::Properties readProperties();
    void readProperty(Tiled::Properties *properties);
};
#include "canvas.h"

